package com.zhouwr.simulator;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @program: forallcn-iotps
 * @description: 模拟器
 * @author: zhouwr
 * @create: 2021-03-08 21:53
 * @version：1.0
 **/
public class SimulatorApplication {
    public static void main(String[] args) {
        new Simulator("127.0.0.1", 8888, 26).run();

    }
}

class Simulator implements Runnable {

    private final byte[] output = {0x03, 0x00};
    private String funcCode;
    private Socket socket = null;

    public Simulator() {
        super();
    }

    public Simulator(String ip, Integer remotePort, Integer localPort) {
        super();
        //创建一个流套接字并将其连接到指定主机上的指定端口号
        try {
            socket = new Socket();
            socket.bind(new InetSocketAddress(localPort));
            socket.connect(new InetSocketAddress(ip, remotePort));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Simulator(String ip, Integer remotePort, Integer localPort, String funcCode) {
        this(ip, remotePort, localPort);
        this.funcCode = funcCode;
    }

    @Override
    public void run() {
        try {
            //读取服务器端数据
            DataInputStream input = new DataInputStream(socket.getInputStream());
            //向服务器端发送数据
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//            out.write(output);

            String ret = input.readUTF();
            System.out.println("服务器端返回过来的是: " + ret);

        } catch (Exception e) {
            System.out.println("客户端异常:" + e.getMessage());
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    socket = null;
                    System.out.println("客户端 finally 异常:" + e.getMessage());
                }
            }
        }
    }

}
